from typing import Generic, TypeVar

from common.message import MesCode

# 暂且理解为,定义一种数据类型,约束数据,例如List[T]约束为一个只能为某单一类型的列表,而不是混合型列表
T = TypeVar('T')


class Response(Generic[T]):  # 泛型类,当实例对象在试图更改属性时候应该保持一致,例如实例化时data:dict,后续要改为int将会破坏类型一致性

    def __init__(self, code: str, data: T, msg: str):
        # data:T,data可以是任意类型
        self.code = code
        self.data = data
        self.msg = msg

    @classmethod
    def success(cls, data=None, msg='OK') -> 'Response':
        return cls(MesCode.success, data, msg)

    @classmethod
    def fail(cls, msg='fail', data=None) -> 'Response':
        return cls(MesCode.fail, data, msg)
